package com.example.recyclerview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {


    private RecyclerView mrecyclerview;//用于瀑布流布局重新设置高度
    private Context mcontext;
    private List<String> datasource;
    private OnItemClickListener clickListener;


    public MyRecyclerViewAdapter(Context context, RecyclerView recyclerView) {
        this.mcontext = context;
        this.datasource = new ArrayList<>();
        this.mrecyclerview = recyclerView;
    }

    public void setDatasource(List<String> datasource) {
        this.datasource = datasource;
        //刷新listview
        notifyDataSetChanged();
    }

    /**
     * 创建并返回viewholder
     *
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.recyclerview_intemlayot, parent, false));
    }

    /**
     * viewholder绑定item数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.miv.setImageResource(geticon(position));
        holder.mtv.setText(datasource.get(position));
        /**
         * 当布局为瀑布流时设置随机高度
         */
        if (mrecyclerview.getLayoutManager().getClass() == StaggeredGridLayoutManager.class) {
            LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getRandomHeight());
            holder.mtv.setLayoutParams(parms);
        } else {
            LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            holder.mtv.setLayoutParams(parms);
        }
    }

    /**
     * 返回不同item随机高度
     *
     * @return
     */
    private int getRandomHeight() {
        return (int) (Math.random() * 1000);
    }

    private int geticon(int position) {
        switch (position % 5) {
            case 0:
                return R.mipmap.a;
            case 1:
                return R.mipmap.b;
            case 2:
                return R.mipmap.c;
            case 3:
                return R.mipmap.d;
            case 4:
                return R.mipmap.e;
        }
        return R.mipmap.a;
    }

    /**
     * 返回数据数量
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return datasource.size();
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public static interface OnItemClickListener {
        void onClick(View view, int position);
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        //实例化item的控件
        ImageView miv;
        TextView mtv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            miv = itemView.findViewById(R.id.img_item);
            mtv = itemView.findViewById(R.id.txt_item);
            //给图片添加点击事件
            miv.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null){
                clickListener.onClick(itemView,getAdapterPosition());
            }
        }
    }
}
